from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
# 1. 手动指定 ChromeDriver 路径
from selenium.webdriver.common.by import By

# 行为链
service = Service('E:/devtools/Python/Python312/chromedriver.exe')  # 替换为实际路径
chrome = webdriver.Chrome(service=service)

try:
    chrome.get("https:/www.baidu.com/")
    cookies = chrome.get_cookies()
    for cookie in cookies:
        print(cookie)
    cookie = chrome.get_cookie('BAIDUID')
    print("一个cookie>>", cookie)
    # 添加cookie
    chrome.add_cookie({'name': 'zhangsan', 'value': '8888888'})
    print(chrome.get_cookie('zhangsan'))
    # 删除指定cookie
    chrome.delete_cookie('zhangsan')
    print(chrome.get_cookie('zhangsan'))
    # 删除浏览器所有cookie
    chrome.delete_all_cookies()
    input("回车键关闭浏览器...")

except Exception as e:
    print(f"执行过程中出错: {e}")
finally:
    chrome.quit()
